package eliauk.fun.flowabledemo.listener.government.countersign;

import eliauk.fun.flowabledemo.FlowableConstant;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("CountersignOneListener")
@Slf4j
public class CountersignOneListener implements TaskListener {

    @Resource
    private RuntimeService runtimeService;

    @Override
    public void notify(DelegateTask delegateTask) {
        String checkResult = (String) delegateTask.getVariable(FlowableConstant.CHECK_RESULT);
        if (checkResult != null) {
            switch (checkResult) {
                case FlowableConstant.PASS:
                    log.info("监听到有人同意了会签");
                    break;
                case FlowableConstant.FAIL:
                    // 如果有人审批不通过，则结束会签任务
                    String processInstanceId = delegateTask.getProcessInstanceId();
                    runtimeService.deleteProcessInstance(processInstanceId, "有人审批不通过，流程结束");
                    break;
            }
        }
    }
}
